package com.westos.userauth.web;

import com.westos.userauth.domain.Party;
import com.westos.userauth.domain.PartyAttribute;
import com.westos.userauth.domain.PartyGroup;
import com.westos.userauth.domain.Person;
import com.westos.userauth.dto.PartyPrintDto;
import com.westos.userauth.dto.PartySearchDto;
import com.westos.userauth.service.PartyService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Date 2020/11/21 14:01
 * @Created by 张佳妮
 */

@RestController
@RequestMapping("/api/party")
public class PartyController {

    @Autowired
    private PartyService partyService;

    /*** 有参和无参构造
     *
     */
    public PartyController() {
    }

    public PartyController(PartyService partyService) {
        this.partyService = partyService;
    }


    /**
     * 删除party---张琳梓
     */
    @GetMapping("/deleteParty")
    public ResponseEntity<String> deleteParty(String partyId) {
        Boolean flag = partyService.deleteParty(partyId);
        if (flag) {
            return new ResponseEntity<String>("删除Party成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("删除Party失败", HttpStatus.METHOD_FAILURE);
        }
    }

    /**
     * 设置状态码--张琳梓
     */
    @GetMapping("/setPartyStatus")
    public ResponseEntity<String> setPartyStatus(Party party, String status) {
        Boolean flag1 = partyService.setPartyStatus(party, status);
        if (flag1) {
            return new ResponseEntity<String>("设置PartyStatus成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("设置PartyStatus失败", HttpStatus.METHOD_FAILURE);
        }
    }

    /**
     * 创建person---张琳梓
     */
    @GetMapping("/createPerson")
    public ResponseEntity<Person> createPerson(Person person) {
        if (person == null) {
            return new ResponseEntity<>((Person) null, HttpStatus.METHOD_FAILURE);
        }
        Person person1 = partyService.createPerson(person);
        return new ResponseEntity<Person>(person1, HttpStatus.OK);
    }

    /**
     * 创建PartyAttribute
     */
    @GetMapping("/createPartyAttribute")
    public ResponseEntity<String> createPartyAttribute(PartyAttribute partyAttribute) {
        Boolean flag2 = partyService.createPartyAttribute(partyAttribute);
        if (flag2) {
            return new ResponseEntity<String>("创建partyAttribute成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("创建partyAttribute失败", HttpStatus.METHOD_FAILURE);
        }
    }


    /*** 更新PartyAttribute————张佳妮
     */
    @GetMapping("/updatePartyAttribute")
    public ResponseEntity<String> updatePartyAttribute(PartyAttribute partyAttribute) {
        Boolean flag = partyService.updatePartyAttribute(partyAttribute);
        if (flag)
            return new ResponseEntity<String>("更新PartyAttribute对象成功", HttpStatus.OK);
        return new ResponseEntity<String>("更新PartyAttribute对象失败", HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /*** 移除PartyAttribute————张佳妮
     */
    @GetMapping("/removePartyAttribute")
    public ResponseEntity<Boolean> removePartyAttribute(PartyAttribute partyAttribute) {
        Boolean aBoolean = partyService.removePartyAttribute(partyAttribute);
        if (aBoolean)
            return new ResponseEntity<Boolean>(true, HttpStatus.OK);
        return new ResponseEntity<Boolean>(false, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /*** 条件查询party————张佳妮
     */
    @GetMapping("/lookupParty")
    public ResponseEntity<List> lookupParty(PartySearchDto partySearchDto) {
        List<PartyPrintDto> list = partyService.lookupParty(partySearchDto);
        if (list == null) {
            return new ResponseEntity(list, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(list, HttpStatus.OK);
    }

    /*** 通过id查询party————张佳妮
     */
    @GetMapping("/findPartiesById")
    public ResponseEntity<String> findPartiesById(String id) {
        PartyPrintDto dto = partyService.findPartiesById(id);
        if (dto == null) {
            return new ResponseEntity<String>("查询数据失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<String>("查询数据成功", HttpStatus.OK);
    }


    /**
     * 李瑞
     */
    @ApiOperation("updatePerson")
    @GetMapping("/updatePerson")
    public ResponseEntity<String> updatePerson(@ModelAttribute("person") Person person) {
        Boolean flag = partyService.updatePerson(person);
        if (flag) {
            return new ResponseEntity<>("更新对象成功!", HttpStatus.OK);
        }
        return new ResponseEntity<>("更新对象失败!", HttpStatus.BAD_REQUEST);
    }

    @ApiOperation("createPartyGroup")
    @GetMapping("/createPartyGroup")
    public ResponseEntity<String> createPartyGroup(@ModelAttribute("partyGroup") PartyGroup partyGroup) {
        PartyGroup partyGroup1 = partyService.createPartyGroup(partyGroup);
        if (partyGroup1 != null) {
            return new ResponseEntity<>("创建对象成功!", HttpStatus.OK);
        }
        return new ResponseEntity<>("创建对象失败!", HttpStatus.BAD_REQUEST);
    }

    @ApiOperation("updatePartyGroup")
    @GetMapping("/updatePartyGroup")
    public ResponseEntity<String> updatePartyGroup(@ModelAttribute("partyGroup") PartyGroup partyGroup) {
        Boolean flag = partyService.updatePartyGroup(partyGroup);
        if (flag) {
            return new ResponseEntity<>("修改用户组成功!", HttpStatus.OK);
        }
        return new ResponseEntity<>("修改用户组失败!", HttpStatus.BAD_REQUEST);
    }
}
